import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

public class CodeReturn extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/jar");
        resp.setHeader("Content-Disposition", "attachment; filename=\"" + "bookCode.jar" + "\"");


        ServletContext servletContext = getServletContext();
            InputStream is = servletContext.getResourceAsStream("/resources/bookCode.jar");

            if(is!=null) {
                int read = 0;
                byte[] bytes = new byte[1024];
                OutputStream os = resp.getOutputStream();
                while((read = is.read(bytes))!=-1) {
                    os.write(bytes,0,read);
                }
                is.close();
                os.flush();
                os.close();

            } else {
                PrintWriter pw  = resp.getWriter();
                pw.println("BOOKCODE.JAR NOT FOUND!");
            }

    }


}
